<template>
  <div class="WxTable">
    <!--                  :height="!autoHeight?'100%':''"
    -->
    <!--  数据展示  height="100%" -->
    <el-table ref="singleTable" :data="tableData" :show-header="true"
              :height="autoHeight ? undefined : '100%'"
              :header-cell-style="{'background-color':'#fff'}">
      <el-table-column v-for="(tb,i) in labels" :label="tb.label" :key="i" :width="tb.width"
                       :show-overflow-tooltip="tb.tooltip||false">
        <template slot-scope="{row}">
          <slot :name="tb.prop" :param="row">{{ row[tb.prop] }}</slot>
        </template>
      </el-table-column>
      <el-table-column :fixed="fixedRight?'right':false" v-if="hasControl"
                       label="操作" :width="controlWidth">
        <template slot-scope="{row}">
          <slot name="control" :param="row">
            <wx-link class="control-btn" :type="e.type||'primary'" v-for="(e,i) in controls"
                     v-if="controls && controls.length>0"
                     :key="i" @click.native="toControl(row,i)">{{ e.label }}
            </wx-link>
          </slot>
        </template>
      </el-table-column>
    </el-table>
    <!--  分页  -->
    <div class="tab-pagination" v-if="pagination">
      <el-pagination
        @current-change="currentChange"
        layout="prev, pager, next, jumper"
        :page-count="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>/**
 *@param labels
 * [{label:'姓名','prop':'name',width:'50',tooltip:'过长文案展示tooltip 默认false'}]
 * 自定义table-item
 * <template #名称="{param}"></template>
 *
 * @param controls:
 * 例子：[{
 *     label: '查看',
 *     type:'el-button type'
 *     fun: 函数（非必填） 优先级高于 controFun
 *   }]
 *
 *   @param controlFun return [tabledata,controls[index]]
 */
import {tableSearch} from "@/api/wxqy/Search";
import WxLink from "@/components/wxqy/wxLink/wxLink";

export default {
  name: "WxTable",
  components: {WxLink},
  props: {
    testData: {
      type: Array,
      default: () => []
    },
    labels: {
      require: true,
      type: Array,
      default: () => []
    },
    controls: {
      type: Array,
      default: () => []
    },
    controFun: Function,
    searchApi: {
      type: String,
      default: null
    },
    pagination: {
      type: Boolean,
      default: true
    },
    sticky: {
      default: false
    },
    fixedRight: { // 允许右浮动
      type: Boolean,
      default: false
    },
    hasControl: { // 允许右浮动
      type: Boolean,
      default: true
    },
    autoHeight: { // 自适应高度
      type: Boolean,
      default: false
    },
    initQuery: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      load: false,
      total: 0,
      tabWidth: 0,
      toComputeTab: false,
      query: this.initQuery || {},
      controlWidth: 50,
      paginationTotal: 10,
      tableData: []
    }
  },
  watch: {
    searchApi: {
      immediate: true,
      handler(v) {
        this.computeControlWidth()
        this.searchTableData(1)
      }
    },
    testData: function (v) {
      this.tableData = v
    }
  },
  methods: {
    computeTabWidth() {
      if (this.toComputeTab) {
        return this.tabWidth > 0 ? this.tabWidth : '100%'
      }
      if (this.labels.length > 0) {
        this.toComputeTab = true
        for (let lb of this.labels) {
          if (!lb.width) {
            this.tabWidth = 0
            return '100%'
          } else {
            this.tabWidth += parseInt(lb.width)
          }
        }
        return this.tabWidth
      }
      return '100%'
    },
    search(params) {
      this.query = params
      this.searchTableData(1)
    },
    searchTableData(page) {
      if (!this.searchApi) {
        console.error('table 请求未知')
        return
      }
      this.load = true
      let opt = Object.assign({pageNum: page}, this.query)
      tableSearch(this.searchApi, opt)
        .then(res => {
          if (res.data) {
            let {rows, pages} = res.data
            this.tableData = rows
            this.total = pages
          }
        }).finally(() => {
        this.load = false
      })
    },
    toControl(e, index) {
      let parentFun = e.Fun || this.controFun
      if (parentFun) {
        parentFun(e, index)
      }
    },
    currentChange(page) {
      this.searchTableData(page)
    },
    computeControlWidth() {
      this.controlWidth = this.controls.length > 3 ? 200 : this.controls.length * 60
    }
  }
}
</script>

<style scoped lang="scss">
.WxTable {
  position: relative;
  height: calc(100% - 70px);
  padding-bottom: 4px;

  .wx-tab-tr {
    background-color: #fff;
  }

  .wx-tab-th {
    font-size: 13px;
    text-align: left;
    line-height: 40px;
    color: #515a6e;

    .wx-tab-th-cell {
      padding-left: 10px;
    }
  }

  .wx-tab-right {
    position: absolute;
    right: 0;
    z-index: 10;
  }
}

.overflow_y:hover::-webkit-scrollbar {
  width: 2px;
  height: 2px;
}

.overflow_y {
  overflow-y: auto;
  height: 100%;

  .wx-table-header-wrapper {
    position: sticky;
    position: -webkit-sticky;
    top: 0;
    z-index: 10;
    border-bottom: 1px solid #d2d2d2;
    background-color: #fff;
  }
}

.wx-tab-wrapper {
  position: relative;

  .wx-table-right {
    position: absolute;
    top: 0;
    right: 0;
    z-index: 1;
  }
}

.tab-pagination {
  margin-top: 10px;
  text-align: center;
  background-color: #fff;
  padding: 10px 20px;
  border-top: 1px solid hsl(0, 0%, 97%);
}

.control-btn {
  margin: 0 4px;
}
</style>
